package com.iwomy.envpro.platform.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.envpro.common.core.util.R;
import com.iwomy.envpro.common.log.annotation.SysLog;
import com.iwomy.envpro.platform.dto.BasicNotificationRecordDTO;
import com.iwomy.envpro.platform.entity.AlarmLoggingEntity;
import com.iwomy.envpro.platform.entity.BasicNotificationRecordEntity;
import com.iwomy.envpro.platform.service.BasicNotificationRecordService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.envpro.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 通知记录
 *
 * @author pig
 * @date 2023-12-01 15:24:54
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basicNotificationRecord")
@Tag(description = "basicNotificationRecord", name = "通知记录管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BasicNotificationRecordController {

	private final BasicNotificationRecordService basicNotificationRecordService;

	/**
	 * 分页查询
	 *
	 * @param page                    分页对象
	 * @param basicNotificationRecord 通知记录
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('platform_basicNotificationRecord_view')")
	public R getBasicNotificationRecordPage(@ParameterObject Page page, @ParameterObject BasicNotificationRecordDTO basicNotificationRecord) {
		LambdaQueryWrapper<BasicNotificationRecordEntity> wrapper = Wrappers.lambdaQuery();

		if (ObjectUtil.isNotNull(basicNotificationRecord.getNotificationType())) {
			wrapper.eq(BasicNotificationRecordEntity::getNotificationType, basicNotificationRecord.getNotificationType());
		}
		if (ObjectUtil.isNotNull(basicNotificationRecord.getConsumeType())) {
			wrapper.eq(BasicNotificationRecordEntity::getConsumeType, basicNotificationRecord.getConsumeType());
		}
		if (ObjectUtil.isNotNull(basicNotificationRecord.getSendResults())) {
			wrapper.eq(BasicNotificationRecordEntity::getSendResults, basicNotificationRecord.getSendResults());
		}

		if (ArrayUtil.isNotEmpty(basicNotificationRecord.getDateTime())) {
			wrapper.ge(BasicNotificationRecordEntity::getCreateTime, basicNotificationRecord.getDateTime()[0]).le(BasicNotificationRecordEntity::getCreateTime,
					basicNotificationRecord.getDateTime()[1]);
		}

		return R.ok(basicNotificationRecordService.page(page, wrapper));
	}


	/**
	 * 通过id查询通知记录
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('platform_basicNotificationRecord_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(basicNotificationRecordService.getById(id));
	}

	/**
	 * 新增通知记录
	 *
	 * @param basicNotificationRecord 通知记录
	 * @return R
	 */
	@Operation(summary = "新增通知记录", description = "新增通知记录")
	@SysLog("新增通知记录")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('platform_basicNotificationRecord_add')")
	public R save(@RequestBody BasicNotificationRecordEntity basicNotificationRecord) {
		return R.ok(basicNotificationRecordService.save(basicNotificationRecord));
	}

	/**
	 * 修改通知记录
	 *
	 * @param basicNotificationRecord 通知记录
	 * @return R
	 */
	@Operation(summary = "修改通知记录", description = "修改通知记录")
	@SysLog("修改通知记录")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('platform_basicNotificationRecord_edit')")
	public R updateById(@RequestBody BasicNotificationRecordEntity basicNotificationRecord) {
		return R.ok(basicNotificationRecordService.updateById(basicNotificationRecord));
	}

	/**
	 * 通过id删除通知记录
	 *
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除通知记录", description = "通过id删除通知记录")
	@SysLog("通过id删除通知记录")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('platform_basicNotificationRecord_del')")
	public R removeById(@RequestBody Long[] ids) {
		return R.ok(basicNotificationRecordService.removeBatchByIds(CollUtil.toList(ids)));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param basicNotificationRecord 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('platform_basicNotificationRecord_export')")
	public List<BasicNotificationRecordEntity> export(BasicNotificationRecordEntity basicNotificationRecord) {
		return basicNotificationRecordService.list(Wrappers.query(basicNotificationRecord));
	}
}